# vim: set autoindent:
import re
import datetime
import time


def _parse_lines(data):
    data = data.split('\n')
    for line in data:
        line = line.strip()

        # Grab missing value.
        if line.startswith('Units'):
            missing = re.search('(-?\d+?\.\d+?) = missing', line).groups()[0]
            missing = float(missing)
            print 'Missing value: %f' % missing
        # Grab depths.
        elif line.startswith('Depth'):
            depths = re.findall('[-\d\.]+', line)
            depths = [float(depth) for depth in depths]
            print 'Depths: %s' % repr(depths)

            # Grab data position.
            indexes = re.findall('[-\d\.]+', line)
	    out = []
	    for pos in indexes:
	    	index = line.index(pos) + len(pos)
		out.append(index)
		line = line.replace(pos, ' ' * len(pos), 1)
	    indexes = out
            indexes.insert(0, 0)
        # Grab data.
        elif re.match('\d{6}', line):
            try:
                dt = time.strptime(line[:13], '%Y%m%d %H%M')
                indexes[0] = 13
            except ValueError:
                dt = time.strptime(line[:8], '%Y%m%d')
                indexes[0] = 8
            dt = datetime.datetime(*dt[:-3])

            values = []
            for start, stop in zip(indexes, indexes[1:]):
                value = line[start:stop].strip()
                value = float(value)
                if value == missing: value = None
                values.append(value)
            
            rest = line[indexes[-1]:].strip()
            
            yield dt, values, depths, rest


def _subsurface_temp(data):  # t
    for dt, values, depths, rest in _parse_lines(data):
        flags = [int(v) for v in rest]

        for value, quality, depth in zip(values, flags, depths):
            yield {'datetime': dt, 'depth': depth, 'value': value, 'quality': quality}


def _salinity(data):  # s
    for dt, values, depths, rest in _parse_lines(data):
        flags, sources = re.split('\s+', rest)
        flags = [int(v) for v in flags]
        sources = [int(v) for v in sources]

        for value, quality, depth, source in zip(values, flags, depths, sources):
            yield {'datetime': dt, 'depth': depth, 'value': value, 'quality': quality, 'source': source}


def _density(data):  # d
    for dt, values, depths, rest in _parse_lines(data):
        flags, sources = re.split('\s+', rest)
        flags = [int(v) for v in flags]
        sources = [int(v) for v in sources]

        for value, quality, depth, source in zip(values, flags, depths, sources):
            yield {'datetime': dt, 'depth': depth, 'value': value, 'quality': quality, 'source': source}
                                                    

def _shortwave_radiation(data):  # rad
    for dt, values, depths, rest in _parse_lines(data):
        value, sd, max = values
        depth = depths[0]

        flags, sources = re.split('\s+', rest)
        flags = [int(v) for v in flags]
        sources = [int(v) for v in sources]

        quality = flags[0]
        source = sources[0]

        yield {'datetime': dt, 'depth': depth, 'value': value, 'sd': sd, 'max': max, 'quality': quality, 'source': source}


def _precipitation(data):  # rain
    for dt, values, depths, rest in _parse_lines(data):
        value, sd, fraction = values
        depth = depths[0]
        
        flags, sources = re.split('\s+', rest)
        flags = [int(v) for v in flags]
        sources = [int(v) for v in sources]

        quality = flags[0]
        source = sources[0]

        yield {'datetime': dt, 'depth': depth, 'value': value, 'sd': sd, 'fraction': fraction, 'quality': quality, 'source': source}


def _isotherm_depth(data):  # iso
    for dt, values, depths, rest in _parse_lines(data):
        flags = [int(v) for v in rest]

        for value, quality in zip(values, flags):
            yield {'datetime': dt, 'value': value, 'quality': quality}


def _meteorology(data):  # met
    for dt, values, depths, rest in _parse_lines(data):
        uwnd, vwnd, wspd, wdir, airt, sst, rh = values
        flags = [int(v) for v in rest]

        yield [{'datetime': dt, 'depth': depths[0], 'value': uwnd},
               {'datetime': dt, 'depth': depths[1], 'value': vwnd},
               {'datetime': dt, 'depth': depths[2], 'value': wspd, 'quality': flags[0]},
               {'datetime': dt, 'depth': depths[3], 'value': wdir, 'quality': flags[1]},
               {'datetime': dt, 'depth': depths[4], 'value': airt, 'quality': flags[2]},
               {'datetime': dt, 'depth': depths[6], 'value': rh, 'quality': flags[4]},
              ]
    

if __name__ == '__main__':
    data = open('t15n38w_dy.ascii').readlines()
    for line in _subsurface_temp(data): print line
